package huang.service;

import huang.dao.CommodityDao;
import huang.domain.Commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Creat by 黄伟洪 on 2021/7/21 11:41
 */
@Service
public class CommodityService {

    /*商品持久层*/
    @Autowired
    CommodityDao commodityDao;

    /*展示所有商品 返回所有商品*/
    public List<Commodity> listAllCommodity(){
        return commodityDao.findAllCommodity();
    }

    public Commodity findCommodityById(Integer c_id){
        return commodityDao.findCommodityById(c_id);
    }
    /*根据信息查找商品*/
    public List<Commodity> findCommodityByNameORType(String msg){
        return commodityDao.findCommodityByNameORType(msg);
    }
    /*新增商品*/
    public boolean addCommodity(Commodity commodity){
        String cName = commodity.getC_name();
        Float cPrice = commodity.getC_price();
        String cType = commodity.getC_type();
        int cSales = commodity.getC_sales();
        int cStock = commodity.getC_stock();
        if (commodityDao.addCommodity(cName,cPrice,cType,cSales,cStock)==1){
            return true;
        }
        return false;
    }
    /*删除商品*/
    public int deleteCommodity(List<Integer> cIds){
        int count=commodityDao.deleteCommoditys(cIds);
        if(count>=1){
            return count;
        }
        return -1;
    }
    /*更新商品*/
    public boolean updateCommodity(Commodity commodity){
        int cId = commodity.getC_id();
        String cName = commodity.getC_name();
        Float cPrice = commodity.getC_price();
        String cType = commodity.getC_type();
        int cSales = commodity.getC_sales();
        int cStock = commodity.getC_stock();
        if (commodityDao.updateCommodity(cId,cName,cPrice,cType,cSales,cStock)==1){
            return true;
        }
        return false;
    }

}
